<?php echo $header; ?>
<link href="view/javascript/jquery/jstree/themes/apple/style.css" rel="stylesheet" type="text/css" id="skinSheet">
<script src="view/javascript/jquery/jstree/jquery.jstree.js" type="text/javascript"></script>
<script type="text/javascript" src="view/javascript/jquery/jstree/lib/jquery.cookie.js"></script>
<!-- <script type="text/javascript" src="view/javascript/jquery/jstree/lib/jquery.metadata.js"></script> -->

<script type="text/javascript" class="source">
  $(function(){
    $("#tree")
    .bind("before.jstree", function (e, data) {
      $("#alog").append(data.func + "<br />");
    })
    .bind("create.jstree", function (e, data) {
       $.post(
           "?route=catalog/category/getCategoriesUseAjax&token=<?php echo $this->session->data['token'];?>",
           {
               "operation" : "create_node",
               "id" : data.rslt.parent.attr("id").replace("node_",""),
               "position" : data.rslt.position,
               "title" : data.rslt.name,
               "type" : data.rslt.obj.attr("rel")
           },
           function (r) {
               if(r.status) {
                   $(data.rslt.obj).attr("id", "node_" + r.id);
                    data.inst.refresh();
               }
               else {
                   $.jstree.rollback(data.rlbk);
                    data.inst.refresh();
               }
           }
       );
     })
    .jstree({
      "plugins" : [ 
        "themes","json_data","ui","crrm","cookies","search","contextmenu","checkbox"
      ],
      "contextmenu": {
        "items":{
          "edit":{
            "label": "edit",  
            "action": function (obj) { 
              window.location.href="?route=catalog/category/update&category_id="+ (obj.attr("id").replace("node_",""))+"&token=<?php echo $this->session->data['token'];?>"
            }  
          },
          "ccp":null
        }
      },
      "themes": {
        "theme": "default",
        "dots" : false,
        "icons" : true
      },  
      "json_data" : { 
        "ajax" : {
          type : "post",
          "url" : "?route=catalog/category/getCategoriesUseAjax&token=<?php echo $this->session->data['token'];?>",//异步请求数据
          "data" : function (n) { 
            return { 
              "operation" : "get_children",
              "id" : n.attr ? n.attr("id").replace("node_","") : 0
            }; 
          }
        }
      },
      "ui" : {
        "initially_select" : [ "/zonda" ]
      },
      "core" : { 
        "initially_open" : [ "/zonda"] 
      }
    })
    .bind("dblclick.jstree", function (event, data) {
        console.debug(event.currentTarget.children);

       


    })

     .bind("remove.jstree", function (e, data) {
      if(confirm("Delete/Uninstall cannot be undone! Are you sure you want to do this? ")){


         data.rslt.obj.each(function () {
             $.ajax({
                 async : false,
                 type: 'post',
                 url: "?route=catalog/category/getCategoriesUseAjax&token=<?php echo $this->session->data['token'];?>",
                 data : {
                     "operation" : "remove_node",
                     "id" : this.id.replace("node_","")
                 },
                 success : function (r) {
                     if(!r.status) {
                         data.inst.refresh();
                     }
                 }
             });
         });
      }else{
        data.inst.refresh();
      }
     })
     .bind("rename.jstree", function (e, data) {
         $.post(
             "?route=catalog/category/getCategoriesUseAjax&token=<?php echo $this->session->data['token'];?>",
             {
                 "operation" : "rename_node",
                 "id" : data.rslt.obj.attr("id").replace("node_",""),
                 "title" : data.rslt.new_name
             },
             function (r) {
                 if(!r.status) {
                    $.jstree.rollback(data.rlbk);
                    data.inst.refresh();
                 }
             }
         );
     })    
  })
$(document).ready(function(){
    $('#content').focus();
  })
  function export_price(){
    var str="";
    var menu = $("#tree").jstree("get_checked");
    if(menu==null){
      alert("No Category selected");
    }else{
      for(var i=0;i<menu.length;i++){
        str+=menu[i].id.replace("node_","")+",";
      }
      if(str){
        location="index.php?route=catalog/category/exportPrice&token=<?php echo $this->session->data['token'];?>&cat_ids="+str;
      }else{
        alert("Choose Category Please ！");
      }      
    }
  }
</script>
<style type="text/css">
  #tree{
    margin: 20px;
  }
</style>

<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?>
    <a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?></div>
  <?php if ($error_warning) { ?>
  <div class="warning">
    <?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success">
    <?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1>
        <img src="view/image/category.png" alt="" />
        <?php echo $heading_title; ?></h1>
     <div class="buttons">
        <a onclick="location = '<?php echo $insert; ?>
          '" class="button">
          <?php echo $button_insert; ?></a> 
        <a onclick="export_price();" class="button">
          Export</a>
      </div> 
    </div>
    <div class="content">
      <form action="<?php echo $delete; ?>
        " method="post" enctype="multipart/form-data" id="form">
        <table>
            <tr></tr>
            <tr>
              <td colspan=4>
                <div id="tree">
                   
                </div>
               
              </td>
            </tr>
        </table>
      </form>
    </div>
  </div>
</div>
<?php echo $footer; ?>